package com.websocket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.service.DroneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

@Component
public class MessageWebSocketHandler extends TextWebSocketHandler {
    private float current_x;
    private float current_y;
    private float current_z;
    private float current_yaw;

    @Autowired DroneService droneService;
    private final CopyOnWriteArrayList<WebSocketSession> sessions = new CopyOnWriteArrayList<>();

    @Override
    public void afterConnectionEstablished(WebSocketSession session) {
        sessions.add(session);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
        sessions.remove(session);
    }

//    public void sendMessage(String message) {
//        for (WebSocketSession session : sessions) {
//            if (session.isOpen()) {
//                try {
//                    session.sendMessage(new TextMessage(message));
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
    public void sendMessage(String message) {
        // 遍历所有会话，如果会话是开放的，则发送消息
        for (WebSocketSession session : sessions) {
            // 如果当前会话是开放的，表示连接仍然有效
            if (session.isOpen()) {
                try {
                    JSONObject jsonObject = JSON.parseObject(message);
                    // 2. 提取字段值
                    this.current_x = jsonObject.getFloatValue("x");
                    this.current_y = jsonObject.getFloatValue("y");
                    this.current_z = jsonObject.getFloatValue("z");
                    this.current_yaw = jsonObject.getFloatValue("yaw");
                    // 创建一个新的TextMessage对象，并将消息内容发送给该会话
                    session.sendMessage(new TextMessage(message));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

//    @Override
//    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
//        String payload = message.getPayload();
//        System.out.println("Received message: " + payload);
//        JSONObject jsonObject = JSON.parseObject(payload);
//
//        // 提取值
//        int x = jsonObject.getIntValue("x");
//        int y = jsonObject.getIntValue("y");
//        int z = jsonObject.getIntValue("z");
//        int r = jsonObject.getIntValue("r");
//
//        droneService.remoteControl(x, y, z, r);
//    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException, InterruptedException {
        // 从TextMessage对象中获取消息内容
        String payload = message.getPayload();
        // 使用FastJSON库将消息内容解析为JSONObject
        JSONObject jsonObject = JSON.parseObject(payload);
        // 解析消息内容，提取`x`, `y`, `z`, `r`的值
        int x = (int)(jsonObject.getIntValue("x")*1.5);
        int y = (int)(jsonObject.getIntValue("y")*1.5);
        int z = jsonObject.getIntValue("z");
        int r = jsonObject.getIntValue("r");
//        System.out.println(jsonObject.toJSONString());
        // 传入提取的值，进行无人机遥控
        droneService.remoteControl(current_x,current_y,current_z,current_yaw,x, y, z, r);
    }
}
